package com.wfcm.wxUitls;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wfcm.entity.WfWxShareEntity;
import com.wfcm.utils.WxUtil;


/** 
 * 定时获取微信access_token的线程 
 *  
 * @author liuyq 
 * @date 2013-05-02 
 */  
public class TokenThread implements Runnable {  
    private static Logger log = LoggerFactory.getLogger(TokenThread.class);  
    // 第三方用户唯一凭证  
    public static String appid = "";  
    // 第三方用户唯一凭证密钥  
    public static String appsecret = "";  
    public static WfWxShareEntity accessToken = new WfWxShareEntity();  
    public void run() {  
        while (true) {  
            try {  
            	accessToken = WxUtil.getAccessToken(appid, appsecret); 
                if (null != accessToken ) {  
                  //  log.info("获取access_token成功，有效时长{}秒 token:{}", accessToken.getExpirationTime(), accessToken.getAccessToken());
                    // 休眠7000秒  
                   // Thread.sleep((accessToken.getExpirationTime() - 200) * 1000);  
                } else {  
                    // 如果access_token为null，60秒后再获取  
                    Thread.sleep(60 * 1000);  
                }  
            } catch (InterruptedException e) {  
                try {  
                    Thread.sleep(60 * 1000);  
                } catch (InterruptedException e1) {  
                    log.error("{}", e1);  
                }  
                log.error("{}", e);  
            }  
        }  
    }  
}  
